﻿using UnityEngine;
using System.Collections;

public class PlayerControl : MonoBehaviour {

    public Animator anim;

    private bool facingRight;
    
    [Tooltip("水平速度")]
    public float h_Speed = 7f;
    [Tooltip ("垂直速度")]
    public float v_Speed = 5f;
    
    void Start () {

	}
	
	
	void Update () {
        if (transform.localScale.x > 0)
            facingRight = true;
        else facingRight = false;
    }

    void FixedUpdate()
    {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        if (Input.GetButton("Horizontal")|| Input.GetButton("Vertical")) 
        {
            anim.SetBool("walking", true);
        }
        else anim.SetBool("walking", false);

        transform.position += new Vector3(h * h_Speed * Time.deltaTime, v * v_Speed * Time.deltaTime, 0);
        
        if (h > 0 && !facingRight)
            Flip();
        else if (h < 0 && facingRight)
            Flip();

    }

    private void Flip()
    {
        transform.localScale = new Vector3(-transform.localScale.x, 1, 1);
    }

}
